package org.limkokwing.hami.ui.form;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;

/**
 * @author Mohammad
 */
public abstract class FormHandler {

    public abstract String getName();

    public abstract String getTitle();

    public abstract void cleanup();

    protected abstract void execute(Form f);

    public abstract String getHelp();

    public final void run(final Command backCommand, ActionListener commandListener) {
        System.gc();

        final Form from = new Form(getName());

        from.addCommand(new Command("Help") {

            public void actionPerformed(ActionEvent evt) {
                Form helpForm = new Form("Help");
                helpForm.setLayout(new BorderLayout());
                TextArea helpText = new TextArea(getHelp(), 5, 10);
                helpText.setEditable(false);
                helpForm.setScrollable(false);
                helpForm.addComponent(BorderLayout.CENTER, helpText);
                Command c = new Command("Back") {

                    public void actionPerformed(ActionEvent evt) {
                        from.show();
                    }
                };
                helpForm.addCommand(c);
                helpForm.setBackCommand(c);
                helpForm.show();
            }
        });
        from.addCommand(backCommand);
        from.addCommandListener(commandListener);
        from.setBackCommand(backCommand);
        execute(from);
        from.show();
    }
}
